/**
 * Created by superman on 17/2/16.
 * http配置
 */

import axios from "axios";
// import store from "../stores/user";
// import * as types from "../store/types";
import router from "../router";

// axios 配置
axios.defaults.timeout = 5000;
axios.defaults.baseURL = "http://apis.juhe.cn/simpleWeather/query";
// axios.defaults.baseURL = "http://aider.meizu.com/app";
// axios.defaults.baseURL = "https://api.github.com";


const token = "";

// http request 拦截器
axios.interceptors.request.use(
  (config) => {
    if (token) {
      config.headers.Authorization = `token ${token}`;
    }
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  (response) => {
    return response;
  },
  (error) => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          // 401 清除token信息并跳转到登录页面
          //   store.commit("LOGOUT"); // vuex的用法

          // 只有在当前路由不是登录页面才跳转
          router.currentRoute.path !== "login" &&
            router.replace({
              path: "login",
              query: { redirect: router.currentRoute.path },
            });
      }
    }
    // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
    return Promise.reject(error.response.data);
  }
);

export default axios;
